package memleak.test.com.testlist;

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;



import java.util.List;

/**
 * Created by runrun on 2017/3/8.
 */

public class ChannelLayout extends RelativeLayout implements OnViewFun<List<LiveChannel>> {
    private static final String TAG = "ChannelLayout";

    private ChannelRecyclerView mChannelRecyclerView;


    private String mCategoryID;

    public ChannelLayout(Context context) {
        this(context, null);
    }

    public ChannelLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChannelLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mChannelRecyclerView = (ChannelRecyclerView) findViewById(R.id.recyclerView);
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        setWillNotDraw(false);
    }

    public String getCategoryID() {
        return mCategoryID;
    }

    public void setCategoryID(String categoryID) {
        mCategoryID = categoryID;
    }


    public void selectItemByNumber(int logicNum) {
        mChannelRecyclerView.selectChannelByNumber(logicNum);
    }

    public LiveChannel getItemByNumber(int logicNum) {
        return mChannelRecyclerView.getChannelByNumber(logicNum);
    }

    /**
     * 直播进入初始化播放的节目
     */
    public void initFocus() {
        int count = mChannelRecyclerView.getChildCount();
        if (count == 0) {
            return;
        }
        PreferencesDataSource preferencesDataSource = PreferencesDataSource.newInstance();
        int cNo = preferencesDataSource.getPreChannelNum();
        String no;
        int size = mChannelRecyclerView.getSize();
        for (int i = 0; i < size; i++) {
            LiveChannel liveChannel = mChannelRecyclerView.getItem(i);
            no = liveChannel.getcNo();
            if (!TextUtils.isEmpty(no) && Integer.parseInt(no) == cNo) {
                LiveApplication app = (LiveApplication) getContext().getApplicationContext();
                app.changeChannelPos(i);
                mChannelRecyclerView.setCurrentItem(i);
                app.adjustPlay(TransformBeanTool.transforVideoBean(liveChannel));
                break;
            }

        }


    }


    @Override
    public void updateView(List<LiveChannel> channelBeen) {
        if (null == channelBeen || channelBeen.isEmpty()) {
            return;
        }
        mChannelRecyclerView.updateDatas(channelBeen);
    }


    public void updateFavoritOnChannelLayout(LiveChannel channel){
        mChannelRecyclerView.updateSingleData(channel);
    }

//    @Override
//    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        if (!changed || !isAttachedToWindow() || View.VISIBLE != getVisibility()) {
//            return;
//        }
//        super.onLayout(changed, l, t, r, b);
//    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!isAttachedToWindow() || View.VISIBLE != getVisibility()) {
            return;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    public void draw(Canvas canvas) {
        if (!isAttachedToWindow() || View.VISIBLE != getVisibility()) {
            return;
        }
        super.draw(canvas);
    }

    @Override
    public void jump() {

    }

    public void setCurrentItem(int channelPos) {
        mChannelRecyclerView.setCurrentItem(channelPos);
    }
    


}
